package homework;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeMap;

class BookComparator implements Comparator<Book> {

    @Override
    public int compare(Book o1, Book o2) {
        return o1.getBid() != o2.getBid() ? o1.getBid() - o2.getBid() : o1.getBname() != o2.getBname() ? 1 : 0;
    }
}

class Book {
    private int bid;
    private String bname;
    private float price;

    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bid=" + bid +
                ", bname='" + bname + '\'' +
                ", price=" + price +
                '}';
    }

    public Book(int bid, String bname, float price) {
        this.bid = bid;
        this.bname = bname;
        this.price = price;
    }
}

public class HomeWork {
    public static void main(String[] args) {
        TreeMap<Book, String> map = new TreeMap<>(new BookComparator());
        map.put(new Book(2, "北游记", 300), "北梁");
        map.put(new Book(1, "西游记", 100), "西梁");
        map.put(new Book(2, "东游记", 200), "东梁");
        map.put(new Book(3, "南游记", 400), "南梁");
        map.put(new Book(4, "中游记", 500), "中梁");
        System.out.println("keySet遍历");
        map.keySet().stream().forEach(x -> System.out.println(x + "," + map.get(x)));
        System.out.println("entrySet遍历");
        map.entrySet().stream().forEach(System.out::println);
        System.out.println("第三题");
        ArrayList<Book> list = new ArrayList<>();
        list.add(new Book(1, "西游记", 100));
        list.add(new Book(4, "南游记", 400));
        list.add(new Book(2, "东游记", 149));
        list.add(new Book(5, "中游记", 500));
        list.add(new Book(3, "北游记", 250));
        list.stream().filter(x -> x.getPrice() < 150).sorted(new BookComparator()).distinct().forEach(System.out::println);
    }
}